﻿@model GitCandy.Models.TreeModel
@using GitCandy.Models
@using LibGit2Sharp;

@{
    ViewBag.Title = String.Format(SR.Shared_TitleFormat, String.Format(SR.Repository_TreeTitle, Model.Name, Model.ReferenceName ?? Model.Commit.Sha.ToShortSha(), Model.Path));
}

<h3>
    @await Html.PartialAsync("_RepositoryLink", Model)
</h3>

@if (Model.Entries == null)
{
    <div>
        @Html.ActionLink(SR.Shared_Detail, "Detail", new{ Model.Id }, new { @class = "btn btn-default pull-right" })
        @await Html.PartialAsync("_GitUrlButton", Model.GitUrls)
    </div>
    <div class="alert alert-info">@SR.Repository_EmptyWords</div>
}
else
{
    <div>
        @if (Model.IsRoot)
        {
            <h5>@Model.Description</h5>
            <div class="border-area">
                <ul class="nav nav-justified">
                    <li>@Html.ActionLink(Model.Scope.Commits + " " + SR.Repository_Commits, "Commits")</li>
                    <li>@Html.ActionLink(Model.Scope.Branches + " " + SR.Repository_Branches, "Branches")</li>
                    <li>@Html.ActionLink(Model.Scope.Tags + " " + SR.Repository_Tags, "Tags")</li>
                    <li>@Html.ActionLink(Model.Scope.Contributors + " " + SR.Repository_Contributors, "Contributors")</li>
                </ul>
            </div>
        }

        <div>
            @if (Model.IsRoot)
            {
                @Html.ActionLink(SR.Shared_Detail, "Detail", new{ Model.Id }, new { @class = "btn btn-default pull-right" })
                @await Html.PartialAsync("_BranchSelector", Model.BranchSelector)
                @await Html.PartialAsync("_ZipButton", Model.ReferenceName ?? Model.Commit.Sha)
                <a href="@Url.Action("Compare", Html.OverRoute(new { path = "" }))" class="btn btn-default"><span class="glyphicon glyphicon-retweet"></span>@SR.Repository_Compare</a>
                @await Html.PartialAsync("_GitUrlButton", Model.GitUrls)
            }
            else
            {
                @await Html.PartialAsync("_BranchSelector", Model.BranchSelector)
                <div class="btn-group">
                    @Html.ActionLink(SR.Repository_History, "Commits", Html.OverRoute(new { path = (Model.ReferenceName ?? Model.Commit.Sha) + "/" + Model.Path }), new { @class = "btn btn-default" })
                </div>
            }
        </div>
        @await Html.PartialAsync("_PathBar", Model.PathBar)
        <div class="border-area table-responsive">
            <table class="table table-hover table-striped">
                <caption>
                    <div class="clearfix">
                        <div title="@Model.Commit.CommitMessageShort" class="pull-left">@Html.ActionLink(Model.Commit.CommitMessageShort.ShortString(80), "Commit", Html.OverRoute(new { path = Model.Commit.Sha }))</div>
                    </div>
                    <div class="clearfix">
                        <strong title="@Model.Commit.Author">@Html.Link(Model.Commit.Author)</strong>
                        <span>@SR.Repository_AuthoredAt</span>
                        <span title="@Model.Commit.Author.When">@Model.Commit.Author.When.LocalDateTime.ToFullString()</span>
                        @if (Model.Commit.Author != Model.Commit.Committer)
                        {
                            <strong title="@Model.Commit.Committer">@Model.Commit.Committer.Name</strong>
                            <span>@SR.Repository_CommittedAt</span>
                            <span title="@Model.Commit.Committer.When">@Model.Commit.Committer.When.LocalDateTime.ToFullString()</span>
                        }
                        <div class="pull-right">@Model.Commit.Sha.ToShortSha()</div>
                    </div>
                </caption>
                <tbody>
                    @foreach (var entry in Model.Entries)
                    {
                        var commit = entry.Commit ?? new CommitModel();
                        var author = commit.Author;
                        <tr>
                            <td>
                                <span title="@entry.Name">
                                    @if (entry.EntryType == TreeEntryTargetType.Tree)
                                    {
                                        <span class="glyphicon glyphicon-folder-close"></span>
                                    }
                                    else if (entry.EntryType == TreeEntryTargetType.Blob)
                                    {
                                        <span class="glyphicon glyphicon-file"></span>
                                    }
                                    else if (entry.EntryType == TreeEntryTargetType.GitLink)
                                    {
                                        <i class="glyphicon glyphicon-bookmark"></i>
                                    }
                                    else
                                    {
                                        <span class="glyphicon glyphicon-leaf"></span>
                                    }
                                    @if (entry.EntryType == TreeEntryTargetType.GitLink)
                                    {
                                        @entry.Name.ShortString(25)
                                    }
                                    else
                                    {
                                        <a href="@Model.GetUrl(entry.EntryType + "", entry.Path)">@entry.Name.ShortString(25)</a>
                                    }
                                </span>
                            </td>
                            <td><span title="@commit.CommitMessageShort">@(commit.Sha == null ? Html.Raw(commit.CommitMessageShort) : Html.ActionLink(commit.CommitMessageShort.ShortString(70), "Commit", Html.OverRoute(new { path = commit.Sha })))</span></td>
                            @if (author != null)
                            {
                                <td title="@author">@author.Name.ShortString(20)</td>
                                <td title="@author.When">@author.When.LocalDateTime.ToFullString()</td>
                            }
                            else
                            {
                                <td></td>
                                <td></td>
                            }
                        </tr>
                    }
                </tbody>
            </table>
        </div>
        @if (Model.Readme != null)
        {
            <div class="border-area">
                <table class="table">
                    <caption>
                        <div class="pull-left keep-space"><i class="glyphicon glyphicon-list-alt"></i> @Model.Readme.Name</div>
                    </caption>
                    <tbody>
                        <tr>
                            <td>
                                @await Html.PartialAsync("_BlobPreview", Model.Readme)
                            </td>
                        </tr>
                    </tbody>
                </table>
            </div>
        }
    </div>
}